$(function() {

	RongIMClient.init("3argexb6re6je");

	// 设置连接监听状态 （ status 标识当前连接状态）
	// 连接状态监听器
	RongIMClient.setConnectionStatusListener({
		onChanged: function(status) {
			switch(status) {
				//链接成功
				case RongIMLib.ConnectionStatus.CONNECTED:
					console.log('链接成功');
					break;
					//正在链接
				case RongIMLib.ConnectionStatus.CONNECTING:
					console.log('正在链接');
					break;
					//重新链接
				case RongIMLib.ConnectionStatus.DISCONNECTED:
					console.log('断开连接');
					break;
					//其他设备登录
				case RongIMLib.ConnectionStatus.KICKED_OFFLINE_BY_OTHER_CLIENT:
					console.log('其他设备登录');
					break;
					//网络不可用
				case RongIMLib.ConnectionStatus.NETWORK_UNAVAILABLE:
					console.log('网络不可用');
					break;
			}
		}
	});

	// 消息监听器
	RongIMClient.setOnReceiveMessageListener({
		// 接收到的消息
		onReceived: function(message) {
			// 判断消息类型
			switch(message.messageType) {
				case RongIMClient.MessageType.TextMessage:
					// 发送的消息内容将会被打印
					console.log(message.content.content);
					appendMessage(message.content.content, "left");
					break;
				case RongIMClient.MessageType.VoiceMessage:
					// 对声音进行预加载                
					// message.content.content 格式为 AMR 格式的 base64 码
					RongIMLib.RongIMVoice.preLoaded(message.content.content);
					break;
				case RongIMClient.MessageType.ImageMessage:
					// do something...
					break;
				case RongIMClient.MessageType.DiscussionNotificationMessage:
					// do something...
					break;
				case RongIMClient.MessageType.LocationMessage:
					// do something...
					break;
				case RongIMClient.MessageType.RichContentMessage:
					// do something...
					break;
				case RongIMClient.MessageType.DiscussionNotificationMessage:
					// do something...
					break;
				case RongIMClient.MessageType.InformationNotificationMessage:
					// do something...
					break;
				case RongIMClient.MessageType.ContactNotificationMessage:
					// do something...
					break;
				case RongIMClient.MessageType.ProfileNotificationMessage:
					// do something...
					break;
				case RongIMClient.MessageType.CommandNotificationMessage:
					// do something...
					break;
				case RongIMClient.MessageType.CommandMessage:
					// do something...
					break;
				case RongIMClient.MessageType.UnknownMessage:
					// do something...
					break;
				default:
					// 自定义消息
					// do something...
			}
		}
	});

	//连接服务器
	var token = app.getState().token;
	console.log("当前token" + token);

	RongIMClient.connect(token, {
		onSuccess: function(userId) {
			console.log("Login successfully." + userId);
		},
		onTokenIncorrect: function() {
			console.log('token无效');
		},
		onError: function(errorCode) {
			console.log(errorCode);
		}
	});
})
var iNow = -1; //用来累加改变左右浮动
function appendMessage(resultMessage, position) {
	var arrIcon = ['img/dog.png', 'img/music.png'];

	var btn = document.getElementById('btn');
	var text = document.getElementById('text');
	var content = document.getElementsByTagName('ul')[0];
	var img = content.getElementsByTagName('img');
	var span = content.getElementsByTagName('span');
	
	iNow++;
	if("left" == position) {
		content.innerHTML += '<li><img src="' + arrIcon[0] + '"><span>' + resultMessage + '</span></li>';
		img[iNow].className += 'imgleft';
		span[iNow].className += 'spanleft';
	} else {
		content.innerHTML += '<li><img src="' + arrIcon[1] + '"><span>' + resultMessage + '</span></li>';
		img[iNow].className += 'imgright';
		span[iNow].className += 'spanright';
	}

	text.value = '';
}

function sendMessage(message, targetName) {
	var msg = new RongIMLib.TextMessage({
		content: message,
		extra: "附加信息"
	});
	var conversationtype = RongIMLib.ConversationType.PRIVATE; // 私聊,其他会话选择相应的消息类型即可。
	mui.get('http://hs.tunnel.javano.cn/getUserByUsername', {
			username: targetName
		},
		function(data) {
			var targetId = data.account;
			RongIMClient.getInstance().sendMessage(conversationtype, targetId, msg, {
				onSuccess: function(message) {
					//message 为发送的消息对象并且包含服务器返回的消息唯一Id和发送消息时间戳
					console.info(message.targetId);
					console.log("Send successfully");
				},
				onError: function(errorCode, message) {
					console.log('发送失败');
				}
			});
		}, 'json');

}